<template>
  <div class="menuDetail">
    <el-card class="box-card">
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="菜单名称:" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="菜单路径:">
          <el-input v-model="form.href"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-select v-model="form.parentId" placeholder="请选择">
            <el-option
              :value="-1"
              label="无上级菜单"
            >
            </el-option>
            <el-option
              v-for="item in menuNodeList"
              :key="item.id"
              :label=item.name
              :value=item.id>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="前端图标" prop="icon">
          <el-input v-model="form.icon"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-radio-group v-model="form.shown">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="form.orderNum"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">提交</el-button>
          <el-button @click="reset" v-show="isShow">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getMenuNodeList, menuSaveOrUpdate, menuEdit } from '@/services/menu'
export default {
  name: 'menuDetail',
  data () {
    return {
      form: {
        name: '',
        href: '',
        parentId: -1,
        description: '',
        icon: '',
        shown: false,
        orderNum: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入菜单名称' }
        ],
        description: [
          { required: true, message: '请输入菜单描述' }
        ],
        icon: [
          { required: true, message: '请输入前端图标' }
        ]
      },
      // 上级数据展示
      menuNodeList: [],
      // 接收 menu 传递过来的 id 值
      id: '',
      // 是否展示重置按钮
      isShow: true
    }
  },
  created () {
    this.getMenuNodeList()
    this.id = this.$route.params.id
    // 添加菜单和编辑菜单共用这个组件,
    // 所以想要访问到编辑菜单的信息,通过判断是否存在id值,
    // 再来决定是否渲染编辑菜单页信息
    if (this.id) {
      this.getMenuEdit()
      // 将重置按钮隐藏
      this.isShow = false
    }
  },
  methods: {
    // 获取 上级菜单 数据
    async getMenuNodeList () {
      const { data } = await getMenuNodeList()
      if (data.code === '000000') {
        this.menuNodeList = data.data
      }
    },
    // 重置表单
    reset () {
      this.form = {}
    },
    // 提交表单信息(保存或新增菜单)
    async onSubmit () {
      await this.$refs.form.validate()
      const { data } = await menuSaveOrUpdate(this.form)
      if (data.code === '000000') {
        this.$message.success('数据提交成功')
        this.$router.push({ name: 'menu' })
      }
    },
    // 获取菜单编辑的详情信息
    async getMenuEdit () {
      const { data } = await menuEdit(this.id)
      if (data.code === '000000') {
        this.form = data.data.menuInfo
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form{
  .el-form-item{
    ::v-deep .el-form-item__label{
      padding-right: 8px !important;
    }
    .el-input{
      margin-left: 8px
    }
  }
}
</style>
